package com.cgnpc.scp.manager.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.manager.mybatis.entity.ProcessInfo;
import com.cgnpc.scp.manager.mybatis.entity.ProcessVisit;
import com.cgnpc.scp.manager.service.IProcessInfoService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/******************************
 * 用途说明: 流程应用列表
 * 作者姓名: chenshijie
 * 创建时间: 2020/08/06 23:04
 ******************************/
@Controller
@RequestMapping("/scp/manager/process")
public class ProcessController{

    private static final Logger LOGGER = LoggerFactory.getLogger(ProcessController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private IProcessInfoService processInfoService;

    /**********************************
     * 用途说明: 保存流程信息
     * 参数说明 processInfo
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="保存流程应用信息")
    @UBA(module = "后台管理", action = "保存流程应用信息", channel = OperatorType.Button)
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult save(HttpServletRequest request, @RequestBody ProcessInfo processInfo) {
        try{
            int updateRows = processInfoService.save(processInfo);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,updateRows);
        }catch (Exception e){
            LOGGER.error("ProcessController save error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 更新流程信息
     * 参数说明 processInfo
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="更新流程信息")
    @UBA(module = "后台管理", action = "编辑流程应用信息", channel = OperatorType.Button)
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult update(HttpServletRequest request, @RequestBody ProcessInfo processInfo) {
        try{
            int updateRows = processInfoService.update(processInfo);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,updateRows);
        }catch (Exception e){
            LOGGER.error("ProcessController update error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 根据id获取流程应用
     * 参数说明 processInfo
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="根据id获取流程应用")
    @UBA(module = "后台管理", action = "流程应用信息", channel = OperatorType.Button)
    @RequestMapping(value = "/get", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult get(HttpServletRequest request, @RequestBody ProcessInfo processInfo) {
        try{
            ProcessInfo p = processInfoService.get(processInfo);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,p);
        }catch (Exception e){
            LOGGER.error("ProcessController get error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 根据id删除流程应用
     * 参数说明 processInfo
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="根据id删除流程应用")
    @UBA(module = "后台管理", action = "删除流程应用信息", channel = OperatorType.Button)
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult delete(HttpServletRequest request, @RequestBody List<String> ids) {
        try{
            int delete = processInfoService.delete(ids);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,delete);
        }catch (Exception e){
            LOGGER.error("ProcessController delete error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 分页查询流程信息
     * 参数说明 processInfo
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="分页查询流程信息")
    @UBA(module = "后台管理", action = "流程应用信息", channel = OperatorType.Page)
    @RequestMapping(value = "/loadByPaging", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult loadByPaging(HttpServletRequest request, @RequestBody ProcessInfo processInfo) {
        try{
            MyPageResult<ProcessInfo> processInfoMyPageResult = processInfoService.loadByPaging(processInfo);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,processInfoMyPageResult);
        }catch (Exception e){
            LOGGER.error("ProcessController loadByPaging error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 更新访问时间
     * 参数说明 processInfo
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="更新访问时间")
    @UBA(module = "后台管理", action = "流程应用信息", channel = OperatorType.Page)
    @RequestMapping(value = "/updateVisitTime", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult updateVisitTime(HttpServletRequest request, @RequestBody ProcessVisit processVisit) {
        try{
            int updateRows = processInfoService.updateVisitTime(processVisit);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,updateRows);
        }catch (Exception e){
            LOGGER.error("ProcessController updateVisitTime error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 分页最近访问流程
     * 参数说明 processInfo
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="分页最近访问流程")
    @UBA(module = "后台管理", action = "流程应用信息", channel = OperatorType.Page)
    @RequestMapping(value = "/loadVisitPaging", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult loadVisitPaging(HttpServletRequest request, @RequestBody ProcessVisit processVisit) {
        try{
            MyPageResult<ProcessVisit> processInfoMyPageResult = processInfoService.loadVisitPaging(processVisit);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,processInfoMyPageResult);
        }catch (Exception e){
            LOGGER.error("ProcessController loadByPaging error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

}
